home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-04 | 8.2 KB | 334 lines | [TEXT/KAHL] |
- /********************************************************* DEFINITION
- DATE: 9/23/93
- AUTHOR: Eric R. Rosé
-
- CLASS: CPPPictButton
-
- SUPERCLASS: CPPVisualObject
-
- This C++ class manages a button which is drawn with pictures
- rather than a control definition.
-
- ********************************************************************/
-
- #include <CPPPictButton.h>
- #include <CPPWindow.h>
-
- extern Rect kDefaultRect;
- extern Rect kEmptyRect;
- extern RGBColor RGBBlack;
- extern RGBColor RGBWhite;
-
- /*-----------------------------------------------------------------*/
- /*------------------------ PUBLIC METHODS -------------------------*/
- /*-----------------------------------------------------------------*/
-
- CPPPictButton::CPPPictButton (CPPWindow *itsWindow, short upPictID,
- short downPictID, Point topLeft,
- Boolean isFramed,
- Boolean canBeTarget,
- Boolean active, Boolean visible) :
- CPPVisualObject (itsWindow, &kDefaultRect, canBeTarget,
- active, visible)
- /* initialize the picture button to work by alternating two pictures */
- {
- if (this->owningWindow)
- {
- if ((this->upPict = GetPicture (upPictID)) != NULL)
- {
- DetachResource ((Handle)this->upPict);
- this->bounds = (**upPict).picFrame;
- OffsetRect (&this->bounds, topLeft.h - this->bounds.left,
- topLeft.v - this->bounds.top);
- }
-
- if ((this->downPict = GetPicture (downPictID)) != NULL)
- DetachResource ((Handle)this->downPict);
- }
-
- this->hasFrame = isFramed;
- this->isEnabled = TRUE;
- this->hiliteType = kUsePicture;
- }
-
- /*-----------------------------------------------------------------*/
-
- CPPPictButton::CPPPictButton (CPPWindow *itsWindow, short thePictID,
- Point topLeft, short hiliteMethod,
- Boolean isFramed,
- Boolean useWindowFont,
- Boolean canBeTarget,
- Boolean active, Boolean visible) :
- CPPVisualObject (itsWindow, &kDefaultRect, canBeTarget,
- active, visible)
- {
- if (this->owningWindow)
- {
- if ((this->upPict = GetPicture (thePictID)) != NULL)
- {
- DetachResource ((Handle)this->upPict);
- this->bounds = (**upPict).picFrame;
- OffsetRect (&this->bounds, topLeft.h - this->bounds.left,
- topLeft.v - this->bounds.top);
- }
- this->downPict = NULL;
- }
-
- this->hasFrame = isFramed;
- this->isEnabled = TRUE;
- this->hiliteType = hiliteMethod;
- }
-
- /*-----------------------------------------------------------------*/
-
- CPPPictButton::~CPPPictButton (void)
- {
- if (this->upPict)
- DisposeHandle ((Handle)this->upPict);
- if (this->upPict)
- DisposeHandle ((Handle)this->downPict);
- }
-
- /*-----------------------------------------------------------------*/
-
- char *CPPPictButton::ClassName (void)
- {
- return "CPPPictButton";
- }
-
- /*-----------------------------------------------------------------*/
-
- Boolean CPPPictButton::DoClick (EventRecord *theEvent)
- /* handle a click in the picture button - if visible */
- {
- Point myPt = theEvent->where;
-
- if (this->IsVisible() && this->isEnabled)
- {
- Global2Local (&myPt);
- if (TrackPictButton(myPt))
- DoOnClick ();
- }
-
- return FALSE;
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPPictButton::Activate (Boolean nowActive)
- /* activate/deactivate our control */
- /* IS OVERRIDE */
- {
- // buttons do not visually change when in background or foreground
-
- CPPVisualObject::Activate(nowActive);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPPictButton::MakeVisible (Boolean nowVisible)
- /* hide/show our button */
- /* IS OVERRIDE */
- {
-
- if (!nowVisible)
- EraseRect (GetBounds());
- InvalRect (GetBounds());
-
- CPPVisualObject::MakeVisible(nowVisible);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPPictButton::MoveContent (short newH, short newV)
- /* move the picture button to a new position */
- {
- OffsetRect (&this->bounds, newH - this->bounds.left,
- newV - this->bounds.top);
- }
-
- /*-----------------------------------------------------------------*/
-
- Rect *CPPPictButton::GetBounds (void)
- /* return the boundsrect of the object */
- /* SUBCLASS MUST OVERRIDE */
- {
- if (this->isVisible)
- return &this->bounds;
- else
- return &kEmptyRect;
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPPictButton::Draw ()
- /* is override */
- {
- PenState OldState;
- Rect frameRect = *GetBounds();
- RGBColor MaskGray = {20000, 20000, 20000};
-
- if (this->IsVisible())
- {
- GetPenState(&OldState);
-
- // draw the button in it's normal state
- if (this->upPict)
- DrawPicture (this->upPict, &frameRect);
-
- // disable it, if necessary
- if (!this->isEnabled)
- {
- if (InColorWindow())
- {
- OpColor (&RGBWhite);
- PenMode (addPin);
- RGBForeColor(&MaskGray);
- PaintRect(&frameRect);
- RGBForeColor(&RGBBlack);
- PenMode (srcCopy);
- }
- else
- {
- PenPat(gray);
- PenMode(patBic);
- PaintRect(&frameRect);
- }
- }
-
- // draw a frame around it, if necessary
- if (this->hasFrame)
- {
- InsetRect(&frameRect, -4, -4);
- PenMode(patCopy);
- PenPat(black);
- PenSize (3,3);
- FrameRect(&frameRect);
- }
-
- SetPenState(&OldState);
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPPictButton::ResizeContent (short newWidth, short newHeight)
- /* change the size of the visible list */
- {
- this->bounds.right = this->bounds.left+newWidth;
- this->bounds.bottom = this->bounds.top+newHeight;
-
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPPictButton::DoOnClick (void)
- /* perform some action when the picture button is clicked */
- /* SUBCLASS SHOULD OVERRIDE */
- {
- if (this->doClickProc) // call the user-specified click handler
- (doClickProc)(this->owningWObject);
- else
- if (this->commandEquivalent != kNoCommand)
- this->owningWObject->DoCommand (this->commandEquivalent);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPPictButton::EnableButton (Boolean nowEnabled)
- {
- if (nowEnabled != this->isEnabled)
- {
- InvalRect (GetBounds());
- this->isEnabled = nowEnabled;
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPPictButton::HilightButton (Boolean doHilight)
- {
- RGBColor MaskGray = {20000, 20000, 20000};
- Rect tempRect = *GetBounds();
- PenState OldState;
-
- GetPenState (&OldState);
-
- if (doHilight)
- {
- switch (this->hiliteType) {
- case kUsePicture :
- if (this->downPict)
- DrawPicture (this->downPict, &tempRect);
- break;
-
- case kUseInvert :
- InvertRect (&tempRect);
- break;
-
- case kUseDarken :
- if (InColorWindow())
- {
- OpColor (&RGBBlack);
- PenMode (subPin);
- RGBForeColor(&MaskGray);
- PaintRect(&tempRect);
- RGBForeColor(&RGBBlack);
- PenMode (srcCopy);
- }
- else
- FillRect (&tempRect, black);
- break;
- }
- }
- else
- {
- if (this->upPict)
- DrawPicture (this->upPict, &tempRect);
- }
-
- SetPenState (&OldState);
-
- }
-
- /*-----------------------------------------------------------------*/
- /*----------------------- PRIVATE METHODS -------------------------*/
- /*-----------------------------------------------------------------*/
-
- Boolean CPPPictButton::TrackPictButton (Point thePoint)
- /* given 'thePoint' in local coordinates, simulate clicking */
- /* on the picture button until the user releases the mouse button */
- {
- Boolean isHilighted = TRUE;
- Point newMouse;
- Rect tempRect = *GetBounds();
-
- if (PtInRect (thePoint, &tempRect))
- {
- HilightButton (isHilighted);
- while (StillDown())
- {
- GetMouse (&newMouse);
- if (PtInRect(newMouse, &tempRect))
- {
- if (!isHilighted)
- HilightButton(isHilighted = TRUE);
- }
- else
- {
- if (isHilighted)
- HilightButton (isHilighted = FALSE);
- }
- }
-
- if (isHilighted)
- HilightButton (FALSE);
- return isHilighted;
-
- }
-
- return FALSE;
- }
-
-
-